/*
 * Copyright (C) 2010 Fred Barrie
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.aergia.vinny.gui.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import org.aergia.vinny.core.ApiResult;
import org.aergia.vinny.database.pojo.VinnyUser;
import org.aergia.vinny.gui.controller.VinnyController;
import org.aergia.vinny.util.IconUtil;

public class LoginDialog extends JDialog implements ActionListener, FocusListener {

	private static final long serialVersionUID = 1L;
	private JButton cancelButton;
	private final VinnyController controller;
	private JLabel imageLabel;
	private JButton loginButton;
	private JPasswordField passwordField;
	private JLabel passwordLabel;
	private JLabel resultLabel;
	private VinnyUser user;
	private JLabel userLabel;
	private JTextField userNameField;

	public LoginDialog(final Frame parent, final VinnyController controller) {
		super(parent, ModalityType.APPLICATION_MODAL);
		this.controller = controller;
		this.initComponents();

		this.pack();
	}

	@Override
	public void actionPerformed(final ActionEvent e) {
		if (e.getSource() == this.loginButton) {
			this.resultLabel.setText("");
			final ApiResult<VinnyUser> userResult = this.controller.validateUser(this.userNameField.getText(),
					new String(this.passwordField.getPassword()));
			if (userResult.isError()) {
				this.resultLabel.setText("Invalid user name or password.");
				this.resultLabel.setForeground(Color.red);
			} else {
				this.user = userResult.getResult();
				this.userNameField.setText("");
				this.passwordField.setText("");
				this.setVisible(false);
			}
		}
		if (e.getSource() == this.cancelButton) {
			this.userNameField.setText("");
			this.passwordField.setText("");
			this.setVisible(false);
		}
	}

	@Override
	public void focusGained(final FocusEvent e) {
		if (e.getSource() == this.passwordField) {
			this.passwordField.setText("");
		}
	}

	@Override
	public void focusLost(final FocusEvent e) {
		// empty
	}

	private JButton getCancelButton() {
		if (this.cancelButton == null) {
			this.cancelButton = new JButton();
			this.cancelButton.setText("Cancel");
			this.cancelButton.addActionListener(this);
		}
		return this.cancelButton;
	}

	private JButton getLoginButton() {
		if (this.loginButton == null) {
			this.loginButton = new JButton();
			this.loginButton.setText("Login");
			this.loginButton.addActionListener(this);
			this.getRootPane().setDefaultButton(this.loginButton);
		}
		return this.loginButton;
	}

	private JPasswordField getPasswordField() {
		if (this.passwordField == null) {
			this.passwordField = new JPasswordField();
			this.passwordField.setColumns(16);
			this.passwordField.setHorizontalAlignment(SwingConstants.LEADING);
			this.passwordField.setEchoChar('•');
		}
		return this.passwordField;
	}

	private JLabel getPasswordLabel() {
		if (this.passwordLabel == null) {
			this.passwordLabel = new JLabel();
			this.passwordLabel.setText("Password");
		}
		return this.passwordLabel;
	}

	private JLabel getResultLable() {
		if (this.resultLabel == null) {
			this.resultLabel = new JLabel();
			this.resultLabel.setText("");
		}
		return this.resultLabel;
	}

	public VinnyUser getUser() {
		return this.user;
	}

	private JLabel getUserLabel() {
		if (this.userLabel == null) {
			this.userLabel = new JLabel();
			this.userLabel.setText("User Name");
		}
		return this.userLabel;
	}

	private JTextField getUserNameField() {
		if (this.userNameField == null) {
			this.userNameField = new JTextField(16);
			this.userNameField.setText("");
		}
		return this.userNameField;
	}

	private void initComponents() {
		this.setTitle("Login");
		this.setFont(new Font("Dialog", Font.PLAIN, 12));
		this.setBackground(Color.white);
		this.setForeground(Color.black);
		this.setLayout(new BorderLayout());

		this.imageLabel = new JLabel(IconUtil.getIcon("/images/64/security.png"));
		final JPanel westPanel = new JPanel(new GridLayout(3, 1));
		westPanel.add(this.imageLabel);
		westPanel.add(new JLabel());
		westPanel.add(new JLabel());

		final JPanel centerPanel = new JPanel(new GridLayout(6, 1));
		centerPanel.add(this.getUserLabel());
		centerPanel.add(this.getUserNameField());
		centerPanel.add(this.getPasswordLabel());
		centerPanel.add(this.getPasswordField());
		centerPanel.add(this.getResultLable());

		final JPanel buttonPanel = new JPanel(new GridLayout(1, 3));
		buttonPanel.add(new JLabel());
		buttonPanel.add(this.getCancelButton());
		buttonPanel.add(this.getLoginButton());
		centerPanel.add(buttonPanel);

		this.add(westPanel, BorderLayout.WEST);
		this.add(centerPanel, BorderLayout.CENTER);

		this.setSize(285, 178);
	}
}
